package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_CREAT_TEAM, desc = "敦煌石窟创建队伍")
public class DunHuangShikuCreateTeamCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuCreateTeamReqMsg req = DunHuangShiKuProto.DunHuangShiKuCreateTeamReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        ret = DunHuangShiKuMgr.canEnterActivityJoinTeamPeriod(player,activityId);
        if(ret != 0){
            return;
        }
        String teamName = StringUtils.clearHeadAndTailEmpty(req.getTeamName());
        ret = DunHuangShiKuMgr.checkTeamName(teamName);
        if(ret != 0){
            return;
        }
        DunHuangShiKuModule dunHuangShiKuModule = player.getModule(DunHuangShiKuModule.class);
        DunHuangShiKuProto.DunHuangShiKuCreateTeamCrossReqMsg.Builder reqCrossMsg = DunHuangShiKuProto.DunHuangShiKuCreateTeamCrossReqMsg.newBuilder();
        // 检查元宝
        Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(DunHuangShiKuMgr.DUNHUANGSHIKU_CREATE_TEAM_NEED_INGOTS));
        if(player.getModule(CurrencyModule.class).currencyIsEnough(property)){
            reqCrossMsg.setHasEnoughMoneyIngots(true);
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
            dunHuangShiKuModule.createTeamProperty  = property;
        }
        reqCrossMsg.setActivityId(req.getActivityId());
        reqCrossMsg.setTeamName(teamName);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_DUN_HUANG_SHI_KU_CREAT_TEAM, reqCrossMsg, reqCrossMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_CREAT_TEAM, builder);
        }
    }
}
